package com.example.community.controller;

import com.example.community.cache.TagCache;
import com.example.community.dto.QuestionDTO;
import com.example.community.service.QuestionService;
import com.example.community.util.CookieChecking;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;

@Controller
public class EditQuestionController {
    @Autowired
    private QuestionService questionService;

    @GetMapping("/publish/{id}")
    public String edit(@PathVariable(name = "id") Long id,
                       Model model, HttpServletRequest request) {
        String createType = questionService.getCreateType(id);
        String token = CookieChecking.CookieToken(request.getCookies(), "token");
        if (!createType.equals(token.substring(0,2))){
            model.addAttribute("message","用户权限错误!");
            return "error";
        }
        QuestionDTO question = questionService.getById(id);
        model.addAttribute("title", question.getTitle());
        model.addAttribute("description", question.getDescription());
        model.addAttribute("tag", question.getTag());
        model.addAttribute("id", question.getId());
        model.addAttribute("tags", TagCache.get());
        return "publish";
    }

    @GetMapping("/publish")
    public String publish(Model model) {
        model.addAttribute("tags", TagCache.get());
        return "publish";
    }


}
